package com.hn658.base.message.managermq.init;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import javax.jms.Destination;

import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.hn658.base.message.appinfo.entity.AppMqConfigEO;
import com.hn658.base.message.appinfo.enums.QueueType;
import com.hn658.base.message.managermq.MessageDestinationManager;
import com.hn658.base.message.managermq.config.provider.MqConfigProvider;
import com.hn658.base.message.managermq.destination.IMessageDestinationBean;
import com.hn658.base.message.managermq.destination.impl.MessageDestinationQueue;
import com.hn658.base.message.managermq.destination.impl.MessageDestinationTopic;
import com.hn658.framework.logging.Logger;
import com.hn658.framework.logging.LoggerFactory;
import com.hn658.framework.logging.enums.LogCategory;

/**
 * 
 * ActiveMq队列目标初始化
 * @author ztjie
 * @date 2015-11-19 下午3:52:01
 * @since
 * @version
 */
@Component
public class ActiveMqDestinationInit implements InitializingBean{
	
	/**
     * 日志记录器
     */
    private static final Logger logger = LoggerFactory.getLogger(ActiveMqDestinationInit.class);
	
    @Autowired
	private MqConfigProvider mqConfigProvider;
	
	private Lock lock = new ReentrantLock();

	@Autowired
	private MessageDestinationManager messageDestinationManager;
	@Override
	public void afterPropertiesSet() throws Exception {
		lock.lock();
		try{
			List<AppMqConfigEO> configList = mqConfigProvider.queryConfig();
			for(AppMqConfigEO config : configList){
				IMessageDestinationBean defaultDestination=null;
				if(config.getQueueType().intValue()==QueueType.Queue.getCode().intValue()){
					defaultDestination = new MessageDestinationQueue();
				}else{
					defaultDestination = new MessageDestinationTopic();
				}
				defaultDestination.setDestinationName(config.getQueueName());
				messageDestinationManager.add(config.getAppId(),config.getQueuePriority(), defaultDestination);
			}		
		}finally{
			lock.unlock();			
		}
	}
	
}
